package com.cat.slidingWindow;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/longest-repeating-character-replacement/
 * @create 2025/9/17 20:31
 * @since JDK17
 */

public class Solution41 {
    public int characterReplacement(String s, int k) {
        char[] arr = s.toCharArray();
        int n = arr.length, ans = 0;
        int[] cnt = new int[128];
        for (int l = 0, r = 0; r < n; r++) {
            cnt[arr[r]]++;  // 进窗口
            while (r - l + 1 - cnt[arr[r]] > k) {
                cnt[arr[l++]]--;    // 出窗口
            }
            ans = Math.max(ans, r - l + 1);
        }

        return ans;
    }
}
